/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-03-19 21:14:53
 * @LastEditors: TT-hzy 
 * @LastEditTime: 2024-05-05 17:28:15
 */

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';

import 'package:get/get.dart';

import '../../utils/index.dart';

import '../../widgets/index.dart';

abstract class BaseGetXController extends GetxController
    with HzyAbstractNetWork, WidgetsBindingObserver, HzyNormalLifeCycleAbs {
  /// 项目中 使用的失败界面的标识,用于使用者 显示不同的界面
  /// 具体 使用,一般是通过网络请求,进行逻辑处理
  CommonPlaceHoldType placeHoldType = CommonPlaceHoldType.emptyData;

  /// 缺省页 描述语
  String? placeMsg;

  /// 缺省页 按钮文字
  String? placeBtnMsg;

  /// 配置界面状态
  /// PageState.initializedState 初始化中 默认状态
  var pageState = PageState.initializedState.obs;

  /// 是否显示导航栏
  var isShowAppBar = true;

  /// 导航栏标题
  var appBarTitle = "";

  /// 导航栏颜色
  Color? navBackgroudColor;

  var isNeedSafe = true;

  /// 默认安全区顶部 忽略
  var safeAreatop = true;

  /// 默认安全区底部 忽略
  var safeAreabottm = false;

  /// 背景颜色
  Color? scallBackGroundColor;

  /// 是否显示Scaffol脚手架
  var isNeedScaffol = true;

  var isNeedPlaceHolder = true;

  /// 滚动属性
  bool? resizeToAvoidBottomInset;

  // 配置界面是否显示 加载界面
  var isShowLoadWidget = false.obs;

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addObserver(this);
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      configWidgetRenderingCompleted();
    });
    configDefault();
  }

  @override
  onClose() {
    WidgetsBinding.instance.removeObserver(this);
    super.onClose();
  }

  /// 界面渲染完成
  configWidgetRenderingCompleted() async {}

  /// 配置默认选项
  configDefault() {}

  /// 配置刷新数据事件
  configReloadData({
    bool isShowLoad = false,
  }) {
    getNetWorkData();
  }

  // 点击缺省页触发事件
  tapPlaceHoldWidgetMethod({
    required CommonPlaceHoldType placeHoldType,
  }) {}

  /// 点击返回按钮触发事件
  tapNormalBack() {
    Navigator.pop(Get.context!);
  }

  @override
  getNetWorkData({
    int? type,
    Map<String, dynamic>? info,
  }) {}

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      configAppLifeCycleResumed();
    } else if (state == AppLifecycleState.inactive) {
      configAppLifeCycleInactive();
    } else if (state == AppLifecycleState.paused) {
      configAppLifeCyclePaused();
    } else if (state == AppLifecycleState.detached) {
      configAppLifeCycleDetached();
    }
  }

  ///当前系统改主题改变回调
  @override
  void didChangePlatformBrightness() {
    super.didChangePlatformBrightness();
    configPlatformBrightness();
  }

  /// 当前系统语言发生改变回调
  @override
  void didChangeLocales(List<Locale>? locales) {
    super.didChangeLocales(locales);
    configChangeLocales(locales);
  }

  /// ------------- 配置监听事件 ---------------- ///
  ///
  ///

  /// app进入前台
  @override
  configAppLifeCycleResumed() {}

  /// app在前台但不响应事件，比如电话，touch id等"
  @override
  configAppLifeCycleInactive() {}

  /// app进入后台
  @override
  configAppLifeCyclePaused() {}

  /// 没有宿主视图但是flutter引擎仍然有效
  @override
  configAppLifeCycleDetached() {}

  /// 主题变更事件回调
  @override
  configPlatformBrightness() {
    // ThemeTool.changeTheme(isUserCache: true);
  }

  /// 语言发生改变回调
  @override
  configChangeLocales(List<Locale>? locales) {}
}

abstract class BaseGetXListController extends BaseGetXController
    with HzyAbstracRefreshMehod {
  EasyRefreshController? refreshController;
  bool controlFinishRefresh = true;
  bool controlFinishLoad = true;

  @override
  @mustCallSuper
  configDefault() {
    configRefreshController();
  }

  @override
  void configRefreshController() {
    refreshController = EasyRefreshController(
      controlFinishLoad: controlFinishLoad,
      controlFinishRefresh: controlFinishRefresh,
    );
  }

  @override
  configReloadData({
    bool isShowLoad = false,
  }) {
    // 如果页面状态为空数据状态
    if (pageState.value == PageState.emptyDataState) {
      // 获取网络数据，参数为3
      getNetWorkData(type: isShowLoad ? 3 : 1);
    } else {
      // 如果refreshController不为null，执行刷新操作
      refreshController?.callRefresh();
    }
  }

  // 上啦加载
  @override
  void configLoading() {
    page++;
    getNetWorkData(
      type: 2,
    );
  }

  // 下啦刷新
  @override
  void configRefresh() {
    page = 1;
    getNetWorkData(
      type: 1,
    );
  }

  @override
  PageState endRefresh({
    required int type,
    required PageState state,
  }) {
    PageState normalState = configEndRefresh(
      type: type,
      state: state,
      refreshController: refreshController,
    );
    if (type == 1 || type == 3) {
      pageState.value = normalState;
    }
    return normalState;
  }
}
